package com.myccb.app.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.myccb.app.R;

/**
 * Created by hyt on 2017/11/25.
 */
public class SubTitleBar extends RelativeLayout {
    private TextView title;
    private TextView content;
    private TextView rightTextView;


    private OnClickListener onRightTextClickListener;


    public SubTitleBar(Context context) {
        super(context);
    }

    public SubTitleBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.subtitle_bar_layout, this, true);
        title = findViewById(R.id.subtitle_bar_left);
        content = findViewById(R.id.subtitle_bar_content);
        rightTextView = findViewById(R.id.subtitle_bar_right);

        rightTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onRightTextClickListener!=null)
                    onRightTextClickListener.onClick(v);
            }
        });
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.SubTitleBar);

        if (attributes != null) {
            //处理titleBar背景色
            int titleBarBackGround = attributes.getResourceId(R.styleable.SubTitleBar_background_color, R.color.transparentWhite);
            setBackgroundResource(titleBarBackGround);

            boolean contentVisible = attributes.getBoolean(R.styleable.SubTitleBar_content_visible, true);
            if (contentVisible) {
                content.setVisibility(View.VISIBLE);
            } else {
                content.setVisibility(View.INVISIBLE);
            }
            String contentText = attributes.getString(R.styleable.SubTitleBar_content_text);
            if (!TextUtils.isEmpty(contentText)) {
                content.setText(contentText);
                int contentTextColor = attributes.getColor(R.styleable.SubTitleBar_content_text_color, Color.GRAY);
                content.setTextColor(contentTextColor);
            } else {
                int contentDrawable = attributes.getResourceId(R.styleable.SubTitleBar_content_drawable, -1);
                if (contentDrawable != -1) {
                    content.setBackgroundResource(contentDrawable);
                }
            }
            //获取文字标题
            String titleText = attributes.getString(R.styleable.SubTitleBar_title);
                if (!TextUtils.isEmpty(titleText)) {
                    title.setText(titleText);
                }
            //获取标题显示颜色
            int titleTextColor = attributes.getColor(R.styleable.SubTitleBar_title_text_color, Color.WHITE);
            title.setTextColor(titleTextColor);

            //处理右边文字
            //获取是否要显示右边文字
            boolean rightTextVisible = attributes.getBoolean(R.styleable.SubTitleBar_right_text_visible, true);
            if (rightTextVisible) {
                rightTextView.setVisibility(View.VISIBLE);
            } else {
                rightTextView.setVisibility(View.INVISIBLE);
            }
            //设置右边的文字
            String rightText = attributes.getString(R.styleable.SubTitleBar_right_text);
            if (!TextUtils.isEmpty(rightText)) {
                rightTextView.setText(rightText);
                //设置右边按钮文字颜色
                int rightTextColor = attributes.getColor(R.styleable.SubTitleBar_title_text_color, getResources().getColor(R.color.colorPrimary));
                rightTextView.setTextColor(rightTextColor);
            }
            attributes.recycle();
        }

    }

    public SubTitleBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public SubTitleBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setOnRightTextClickListener(OnClickListener onRightTextClickListener) {
        this.onRightTextClickListener = onRightTextClickListener;
    }
}
